package com.example.core.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.core.entity.SysUser;
import com.example.core.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class SysUserController extends ApiController {

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/import")
    public R importExcel() throws IOException {
        File file = new File("C:\\Users\\admin\\Desktop\\填报--2020年经销商经营情况报表.xlsx");//填报--2020年经销商经营情况报表//
        InputStream inputStream = new FileInputStream(file);
        Workbook workbook = WorkbookFactory.create(inputStream);
        Sheet sheet = workbook.getSheetAt(1);
        List<Map<String, String>> formatList = new ArrayList<>();


        return success(formatList);
    }


    /**
     * @param cell 对象
     * @return 处理后的值
     */
    public static String getCellValue(Cell cell) {
        String cellValue = "";
        if (cell != null) {
            switch (cell.getCellType()) {
                case NUMERIC: // 数字
                    cellValue = String.valueOf(cell.getNumericCellValue());
                    break;
                case STRING: // 字符串
                    cellValue = String.valueOf(cell.getStringCellValue());
                    break;
                case BOOLEAN: // Boolean
                    cellValue = String.valueOf(cell.getBooleanCellValue());
                    break;
                case FORMULA: // 公式
                    cellValue = NumberToTextConverter.toText(cell.getNumericCellValue());
                    break;
                case BLANK: // 空值
                    cellValue = "";
                    break;
                case ERROR: // 故障
                    cellValue = "非法字符";
                    break;
                default:
                    cellValue = "未知类型";
                    break;
            }
            return cellValue;
        } else {
            return cellValue;
        }

    }


    @RequestMapping("/sysUser")
    public R selectAll(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                       @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> dataList = sysUserService.queryPage();
        PageInfo<SysUser> pageInfo = new PageInfo<>(dataList);
        return success(pageInfo);
    }
}